using System;
using System.Collections.Generic;
using System.Text;

using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Design;
using System.Collections;
using System.Windows.Forms;
 
namespace Adoor.VSDesigner.CollectionEditor
{
    public class BaseDataCollectionEditor : System.ComponentModel.Design.CollectionEditor
    {
        public BaseDataCollectionEditor(Type type)
            : base(type)

        {
            this.editValueMask = -1;
        }

        protected override bool CanSelectMultipleInstances()
        {
            return false;
        }



        protected override System.ComponentModel.Design.CollectionEditor.CollectionForm CreateCollectionForm()
        {
            this.form = new BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm(this);
            return this.form;
        }



        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            this.propObject = new ReadOnlyObjectWrapper();
            if (this.form != null)
            {
                this.form.ResetListUI();
                this.form.Owner = null;
            }
            object obj1 = base.EditValue(context, provider, value);
            this.editValueMask = -1;
            this.propObject = null;
            return obj1;
        }

        protected virtual void OnEditButtonClicked(object selectedObject)
        {
        }

        protected virtual void OnFormCreated()
        {
            this.DisplayedPropertyName = "Name";
        }

        protected virtual void OnInstanceCreatedAndAdded(object instance, ArrayList createdItems)
        {
        }

        protected virtual void OnInstanceOrderSwapped(int offset1, int offset2)
        {
        }

        protected void RefreshContents()
        {
            if (this.form != null)
            {
                this.form.RefreshContents();
            }
        }

        protected override object SetItems(object editValue, object[] value)
        {
            return editValue;
        }
        protected string DisplayedPropertyName
        {
            get
            {
                return this.displayedPropertyName;
            }
            set
            {
                this.displayedPropertyName = value;
            }
        }
        protected int EditValueMask
        {
            get
            {
                return this.editValueMask;
            }
            set
            {
                this.editValueMask = value;
            }
        }
        protected virtual string FormCaption
        {
            get
            {
                return "Caption"+ base.CollectionItemType.Name; 
            }
        }

        protected virtual string[] NewItemNames
        {
            get
            {
                return new string[0];
            }
        }

        private string displayedPropertyName;
        private int editValueMask;
        private BaseDataCollectionEditorCollectionForm form;
        private ReadOnlyObjectWrapper propObject;


        protected class BaseDataCollectionEditorCollectionForm : System.ComponentModel.Design.CollectionEditor.CollectionForm
        {
            static BaseDataCollectionEditorCollectionForm()
            {
                BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.LOG10 = Math.Log(10);
            }

            public BaseDataCollectionEditorCollectionForm(BaseDataCollectionEditor editor)
                : base(editor)
            {
                this.editor = editor;
                this.Initialize();
                editor.OnFormCreated();
            }

            private void AddButton_click(object sender, EventArgs e)
            {
                this.CreateAndAddInstance(base.NewItemTypes[0]);
            }

            private void AddDownButton_click(object sender, EventArgs e)
            {
                Point point1 = this.addButton.Location;
                for (Control control1 = this.addButton.Parent; (control1 != null) && (control1 != this); control1 = control1.Parent)
                {
                    point1.Offset(control1.Location);
                }
                point1.Y += this.addButton.Height;
                this.addDownMenu.Show(this, point1);
            }

            private void AddDownMenu_click(object sender, EventArgs e)
            {
                if (sender is BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.TypeMenuItem)
                {
                    BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.TypeMenuItem item1 = (BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.TypeMenuItem)sender;
                    this.CreateAndAddInstance(item1.ItemType);
                }
            }

            private void AdjustListBoxItemHeight()
            {
                this.listbox.ItemHeight = this.listbox.Font.Height + (SystemInformation.BorderSize.Width * 2);
            }

            private void BaseDataCollectionEditor_HelpButtonClicked(object sender, CancelEventArgs e)
            {
                e.Cancel = true;
                this.editor.ShowHelp();
            }

            private void CreateAndAddInstance(Type type)
            {
                try
                {
                    this.RaiseComponentChange(true);
                    object obj1 = base.CreateInstance(type);
                    this.RaiseComponentChange(false);
                    if (obj1 == null)
                    {
                        return;
                    }
                    int num1 = ((int)(Math.Log((double)this.listbox.Items.Count) / BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.LOG10)) + 1;
                    this.editor.OnInstanceCreatedAndAdded(obj1, null);
                    BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.ListItem item1 = new BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.ListItem(obj1, this.GetDisplayText(obj1));
                    this.listbox.Items.Add(item1);
                    int num2 = ((int)(Math.Log((double)this.listbox.Items.Count) / BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.LOG10)) + 1;
                    if (num1 != num2)
                    {
                        this.listbox.Invalidate();
                    }
                    this.listbox.ClearSelected();
                    this.listbox.SelectedIndex = this.listbox.Items.IndexOf(item1);
                }
                catch (CheckoutException)
                {
                }
                catch (Exception exception1)
                {
                    this.DisplayError(exception1);
                }
            }

            private void DownButton_click(object sender, EventArgs e)
            {
                try
                {
                    int num1 = this.listbox.SelectedIndex;
                    if (num1 == (this.listbox.Items.Count - 1))
                    {
                        return;
                    }
                    this.editor.OnInstanceOrderSwapped(num1, num1 + 1);
                    int num2 = this.listbox.TopIndex;
                    object obj1 = this.listbox.Items[num1];
                    this.listbox.Items[num1] = this.listbox.Items[num1 + 1];
                    this.listbox.Items[num1 + 1] = obj1;
                    if (num2 < (this.listbox.Items.Count - 1))
                    {
                        this.listbox.TopIndex = num2 + 1;
                    }
                    this.listbox.ClearSelected();
                    this.listbox.SelectedIndex = num1 + 1;
                }
                catch (CheckoutException)
                {
                }
                catch (Exception exception1)
                {
                    this.DisplayError(exception1);
                }
            }

            private void editButton_Click(object sender, EventArgs e)
            {
            }


            private void Form_HelpRequested(object sender, HelpEventArgs e)
            {
                this.editor.ShowHelp();
                e.Handled = true;
            }



            private string GetDisplayText(object value)
            {
                string text1;
                if (value == null)
                {
                    return string.Empty;
                }
                PropertyDescriptor descriptor1 = TypeDescriptor.GetProperties(value)[this.editor.DisplayedPropertyName];
                if (descriptor1 != null)
                {
                    text1 = (string)descriptor1.GetValue(value);
                    if ((text1 != null) && (text1.Length > 0))
                    {
                        return text1;
                    }
                }
                descriptor1 = TypeDescriptor.GetDefaultProperty(base.CollectionType);
                if ((descriptor1 != null) && (descriptor1.PropertyType == typeof(string)))
                {
                    text1 = (string)descriptor1.GetValue(base.EditValue);
                    if ((text1 != null) && (text1.Length > 0))
                    {
                        return text1;
                    }
                }
                text1 = TypeDescriptor.GetConverter(value).ConvertToString(value);
                if ((text1 != null) && (text1.Length != 0))
                {
                    return text1;
                }
                return value.GetType().Name;
            }

            protected virtual void Initialize()
            {
                Type[] typeArray1 = base.NewItemTypes;
                bool flag1 = (this.editor.editValueMask & 0x40) == 0;
                bool flag2 = typeArray1.Length > 1;
                bool flag3 = (this.editor.editValueMask & 1) != 0;
                this.InitializeComponent();
                this.propertyBrowser.CommandsVisibleIfAvailable = false;
                this.addButton.Enabled = (this.editor.editValueMask & 2) != 0;
                this.editButton.Enabled = (this.editor.editValueMask & 4) != 0;
                this.removeButton.Enabled = (this.editor.editValueMask & 8) != 0;
                this.upButton.Enabled = (this.editor.editValueMask & 0x20) != 0;
                this.downButton.Enabled = (this.editor.editValueMask & 0x20) != 0;
                if (!this.upButton.Enabled)
                {
                    this.upButton.Visible = false;
                    this.downButton.Visible = false;
                }
                if (!flag3)
                {
                    this.propertyBrowser.Enabled = false;
                    this.propertiesLabel.Enabled = false;
                }
                if (!flag1)
                {
                    this.editButton.Visible = false;
                }
                if (flag2)
                {
                    Bitmap bitmap1 = new Icon(typeof(Button), "Arrow.ico").ToBitmap();
                    bitmap1.MakeTransparent();
                    this.addButton.Image = bitmap1;
                    this.addButton.TextImageRelation = TextImageRelation.TextBeforeImage;
                    this.addButton.Click += new EventHandler(this.AddDownButton_click);
                    EventHandler handler1 = new EventHandler(this.AddDownMenu_click);
                    string[] textArray1 = this.NewItemNames;
                    for (int num1 = 0; num1 < typeArray1.Length; num1++)
                    {
                        if (textArray1.Length > 0)
                        {
                            this.addDownMenu.MenuItems.Add(new BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.TypeMenuItem(textArray1[num1], typeArray1[num1], handler1));
                        }
                        else
                        {
                            this.addDownMenu.MenuItems.Add(new BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.TypeMenuItem(typeArray1[num1], handler1));
                        }
                    }
                }
                else
                {
                    this.addButton.Image = null;
                    this.addButton.Click += new EventHandler(this.AddButton_click);
                }
                this.Text = this.editor.FormCaption;
                this.listbox.SelectionMode = this.CanSelectMultipleInstances() ? SelectionMode.MultiExtended : SelectionMode.One;
                this.AdjustListBoxItemHeight();
                this.listbox.KeyDown += new KeyEventHandler(this.Listbox_keyPress);
                this.listbox.DrawItem += new DrawItemEventHandler(this.Listbox_drawItem);
                this.listbox.SelectedIndexChanged += new EventHandler(this.Listbox_selectedIndexChanged);
                this.removeButton.Click += new EventHandler(this.RemoveButton_click);
                this.upButton.Click += new EventHandler(this.UpButton_click);
                this.downButton.Click += new EventHandler(this.DownButton_click);
                this.okButton.Click += new EventHandler(this.OKButton_click);
                base.HelpRequested += new HelpEventHandler(this.Form_HelpRequested);
                this.propertyBrowser.PropertyValueChanged += new PropertyValueChangedEventHandler(this.PropertyGrid_propertyValueChanged);
                this.editButton.Click += new EventHandler(this.OnEditButtonClicked);
            }

            private void InitializeComponent()
            {
                ComponentResourceManager manager1 = new ComponentResourceManager(typeof(BaseDataCollectionEditor));
                this.okButton = new Button();
                this.addButton = new Button();
                this.upButton = new Button();
                this.removeButton = new Button();
                this.propertyBrowser = new PropertyGrid();
                this.editButton = new Button();
                this.downButton = new Button();
                this.listbox = new ListBox();
                this.membersLabel = new Label();
                this.propertiesLabel = new Label();
                this.overarchingTableLayoutPanel = new TableLayoutPanel();
                this.addEditTableLayoutPanel = new TableLayoutPanel();
                this.addDownMenu = new ContextMenu();
                this.overarchingTableLayoutPanel.SuspendLayout();
                this.addEditTableLayoutPanel.SuspendLayout();
                base.SuspendLayout();
                manager1.ApplyResources(this.okButton, "okButton");
                this.overarchingTableLayoutPanel.SetColumnSpan(this.okButton, 4);
                this.okButton.DialogResult = DialogResult.OK;
                this.okButton.Margin = new Padding(3, 3, 0, 0);
                this.okButton.Name = "okButton";
                manager1.ApplyResources(this.addButton, "addButton");
                this.addButton.Margin =new Padding(0, 0, 3, 0);
                this.addButton.Name = "addButton";
                manager1.ApplyResources(this.upButton, "upButton");
                this.upButton.Margin =new Padding(6, 3, 0x12, 0);
                this.upButton.Name = "upButton";
                manager1.ApplyResources(this.removeButton, "removeButton");
                this.removeButton.Margin =new Padding(6, 3, 0x12, 3);
                this.removeButton.Name = "removeButton";
                manager1.ApplyResources(this.propertyBrowser, "propertyBrowser");
                this.propertyBrowser.BackColor = SystemColors.Window;
                this.propertyBrowser.CommandsBackColor = SystemColors.Window;
                this.propertyBrowser.CommandsVisibleIfAvailable = false;
                this.propertyBrowser.LineColor = SystemColors.ScrollBar;
                this.propertyBrowser.Margin =new Padding(3, 3, 0, 6);
                this.propertyBrowser.Name = "propertyBrowser";
                this.overarchingTableLayoutPanel.SetRowSpan(this.propertyBrowser, 4);
                this.propertyBrowser.ToolbarVisible = false;
                manager1.ApplyResources(this.editButton, "editButton");
                this.editButton.Margin =new Padding(3, 0, 0, 0);
                this.editButton.Name = "editButton";
                this.editButton.Click += new EventHandler(this.editButton_Click);
                manager1.ApplyResources(this.downButton, "downButton");
                this.downButton.Margin =new Padding(6, 1, 0x12, 3);
                this.downButton.Name = "downButton";
                manager1.ApplyResources(this.listbox, "listbox");
                this.overarchingTableLayoutPanel.SetColumnSpan(this.listbox, 2);
                this.listbox.DrawMode = DrawMode.OwnerDrawFixed;
                this.listbox.FormattingEnabled=true;
                this.listbox.Margin =new Padding(0, 3, 0, 3);
                this.listbox.Name = "listbox";
                this.overarchingTableLayoutPanel.SetRowSpan(this.listbox, 3);
                manager1.ApplyResources(this.membersLabel, "membersLabel");
                this.overarchingTableLayoutPanel.SetColumnSpan(this.membersLabel, 2);
                this.membersLabel.Margin =new Padding(0, 0, 3, 0);
                this.membersLabel.Name = "membersLabel";
                manager1.ApplyResources(this.propertiesLabel, "propertiesLabel");
                this.propertiesLabel.AutoEllipsis=true;
                this.propertiesLabel.Margin =new Padding(3, 0, 0, 0);
                this.propertiesLabel.Name = "propertiesLabel";
                manager1.ApplyResources(this.overarchingTableLayoutPanel, "overarchingTableLayoutPanel");
                this.overarchingTableLayoutPanel.ColumnStyles.Add(new ColumnStyle());
                this.overarchingTableLayoutPanel.ColumnStyles.Add(new ColumnStyle());
                this.overarchingTableLayoutPanel.ColumnStyles.Add(new ColumnStyle());
                this.overarchingTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f));
                this.overarchingTableLayoutPanel.Controls.Add(this.addEditTableLayoutPanel, 0, 4);
                this.overarchingTableLayoutPanel.Controls.Add(this.membersLabel, 0, 0);
                this.overarchingTableLayoutPanel.Controls.Add(this.okButton, 0, 5);
                this.overarchingTableLayoutPanel.Controls.Add(this.downButton, 2, 2);
                this.overarchingTableLayoutPanel.Controls.Add(this.removeButton, 2, 3);
                this.overarchingTableLayoutPanel.Controls.Add(this.upButton, 2, 1);
                this.overarchingTableLayoutPanel.Controls.Add(this.propertiesLabel, 3, 0);
                this.overarchingTableLayoutPanel.Controls.Add(this.propertyBrowser, 3, 1);
                this.overarchingTableLayoutPanel.Controls.Add(this.listbox, 0, 1);
                this.overarchingTableLayoutPanel.Name = "overarchingTableLayoutPanel";
                this.overarchingTableLayoutPanel.RowStyles.Add(new RowStyle());
                this.overarchingTableLayoutPanel.RowStyles.Add(new RowStyle());
                this.overarchingTableLayoutPanel.RowStyles.Add(new RowStyle());
                this.overarchingTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100f));
                this.overarchingTableLayoutPanel.RowStyles.Add(new RowStyle());
                this.overarchingTableLayoutPanel.RowStyles.Add(new RowStyle());
                manager1.ApplyResources(this.addEditTableLayoutPanel, "addEditTableLayoutPanel");
                this.overarchingTableLayoutPanel.SetColumnSpan(this.addEditTableLayoutPanel, 2);
                this.addEditTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50f));
                this.addEditTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50f));
                this.addEditTableLayoutPanel.Controls.Add(this.addButton, 0, 0);
                this.addEditTableLayoutPanel.Controls.Add(this.editButton, 1, 0);
                this.addEditTableLayoutPanel.Margin =new Padding(0, 3, 0, 6);
                this.addEditTableLayoutPanel.Name = "addEditTableLayoutPanel";
                this.addEditTableLayoutPanel.RowStyles.Add(new RowStyle());
                manager1.ApplyResources(this, "$this");
                base.AcceptButton = this.okButton;
                base.CancelButton = this.okButton;
                base.Controls.Add(this.overarchingTableLayoutPanel);
                base.HelpButton = true;
                base.MaximizeBox = false;
                base.MinimizeBox = false;
                base.Name = "BaseDataCollectionEditor";
                base.AutoScaleMode = AutoScaleMode.Font;
                base.ShowIcon=false;
                base.ShowInTaskbar = false;
                base.HelpButtonClicked+= new CancelEventHandler(this.BaseDataCollectionEditor_HelpButtonClicked);
                this.overarchingTableLayoutPanel.ResumeLayout(false);
                this.overarchingTableLayoutPanel.PerformLayout();
                this.addEditTableLayoutPanel.ResumeLayout(false);
                this.addEditTableLayoutPanel.PerformLayout();
                base.ResumeLayout(false);
            }


            private void Listbox_drawItem(object sender, DrawItemEventArgs e)
            {
                if (e.Index != -1)
                {
                    BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.ListItem item1 = (BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.ListItem)this.listbox.Items[e.Index];
                    Graphics graphics1 = e.Graphics;
                    int num2 = ((int)(Math.Log((double)this.listbox.Items.Count) / BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.LOG10)) + 1;
                    int num3 = (4 + (num2 * (this.Font.Height / 2))) + (SystemInformation.BorderSize.Width * 4);
                    Rectangle rectangle1 = new Rectangle(e.Bounds.X, e.Bounds.Y, num3, e.Bounds.Height);
                    ControlPaint.DrawButton(graphics1, rectangle1, ButtonState.Normal);
                    rectangle1.Inflate(-SystemInformation.BorderSize.Width * 2, -SystemInformation.BorderSize.Height * 2);
                    int num4 = num3;
                    Color color1 = SystemColors.Window;
                    Color color2 = SystemColors.WindowText;
                    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                    {
                        color1 = SystemColors.Highlight;
                        color2 = SystemColors.HighlightText;
                    }
                    Rectangle rectangle2 = new Rectangle(e.Bounds.X + num4, e.Bounds.Y, e.Bounds.Width - num4, e.Bounds.Height);
                    graphics1.FillRectangle(new SolidBrush(color1), rectangle2);
                    if ((e.State & DrawItemState.Focus) == DrawItemState.Focus)
                    {
                        ControlPaint.DrawFocusRectangle(graphics1, rectangle2);
                    }
                    num4 += 2;
                    if ((item1.Editor != null) && item1.Editor.GetPaintValueSupported())
                    {
                        Rectangle rectangle3 = new Rectangle(e.Bounds.X + num4, e.Bounds.Y + 1, 20, e.Bounds.Height - 3);
                        graphics1.DrawRectangle(SystemPens.ControlText, rectangle3.X, rectangle3.Y, rectangle3.Width - 1, rectangle3.Height - 1);
                        rectangle3.Inflate(-1, -1);
                        item1.Editor.PaintValue(item1.Value, graphics1, rectangle3);
                        num4 += 0x1b;
                    }
                    StringFormat format1 = new StringFormat();
                    format1.Alignment = StringAlignment.Center;
                    graphics1.DrawString(e.Index.ToString(), this.Font, SystemBrushes.ControlText, (RectangleF)new Rectangle(e.Bounds.X, e.Bounds.Y, num3, e.Bounds.Height), format1);
                    Brush brush1 = new SolidBrush(color2);
                    string text1 = this.GetDisplayText(item1.Value);
                    graphics1.DrawString(text1, this.Font, brush1, (RectangleF)new Rectangle(e.Bounds.X + num4, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
                    brush1.Dispose();
                    format1.Dispose();
                    SizeF ef1 = graphics1.MeasureString(text1, this.Font);
                    int num5 = num4 + ((int)ef1.Width);
                    if ((num5 > e.Bounds.Width) && (this.listbox.HorizontalExtent < num5))
                    {
                        this.listbox.HorizontalExtent = num5;
                    }
                }
            }



            private void Listbox_keyPress(object sender, KeyEventArgs kevent)
            {
                switch (kevent.KeyData)
                {
                    case Keys.Insert:
                        {
                            if ((this.editor.editValueMask & 2) != 0)
                            {
                                this.AddButton_click(this.removeButton, EventArgs.Empty);
                            }
                            return;
                        }
                    case Keys.Delete:
                        {
                            if ((this.editor.editValueMask & 8) != 0)
                            {
                                this.RemoveButton_click(this.removeButton, EventArgs.Empty);
                            }
                            return;
                        }
                }
            }



            private void Listbox_selectedIndexChanged(object sender, EventArgs e)
            {
                this.UpdateEnabled();
            }



            protected void OKButton_click(object sender, EventArgs e)
            {
                this.listbox.Items.Clear();
            }



            protected override void OnActivated(EventArgs e)
            {
                base.OnActivated(e);
                if (this.listbox.SelectedIndex != -1)
                {
                    this.UpdateEnabled();
                }
            }



            protected override void OnClosed(EventArgs e)
            {
                this.OKButton_click(null, null);
                base.DialogResult = DialogResult.OK;
            }



            private void OnEditButtonClicked(object sender, EventArgs e)
            {
                BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.ListItem item1 = (BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.ListItem)this.listbox.Items[this.listbox.SelectedIndex];
                object obj1 = item1.Value;
                this.editor.OnEditButtonClicked(obj1);
            }



            protected override void OnEditValueChanged()
            {
                object obj1 = null;
                if (this.listbox.SelectedIndex != -1)
                {
                    obj1 = ((BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.ListItem)this.listbox.Items[this.listbox.SelectedIndex]).Value;
                }
                this.listbox.Items.Clear();
                if (base.EditValue != null)
                {
                    this.propertyBrowser.Site = new PropertyGridSite(base.Context, this.propertyBrowser);
                    object[] objArray1 = base.Items;
                    for (int num1 = 0; num1 < objArray1.Length; num1++)
                    {
                        this.listbox.Items.Add(new BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.ListItem(objArray1[num1], this.GetDisplayText(objArray1[num1])));
                    }
                    this.listbox.SelectedIndex = -1;
                    if (this.listbox.Items.Count > 0)
                    {
                        int num2 = 0;
                        for (int num3 = 0; (obj1 != null) && (num3 < this.listbox.Items.Count); num3++)
                        {
                            if (((BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.ListItem)this.listbox.Items[num3]).Value == obj1)
                            {
                                num2 = num3;
                                break;
                            }
                        }
                        this.listbox.SelectedIndex = num2;
                    }
                    this.UpdateEnabled();
                }
                this.AdjustListBoxItemHeight();
            }


            protected override void OnFontChanged(EventArgs e)
            {
                base.OnFontChanged(e);
                this.AdjustListBoxItemHeight();
            }


            private void PropertyGrid_propertyValueChanged(object sender, PropertyValueChangedEventArgs e)
            {
                this.ResetListUI();
                this.listbox.Invalidate();
                this.propertiesLabel.Text = "Properties "+ this.GetDisplayText(((BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.ListItem)this.listbox.SelectedItem).Value) ;
            }


            protected void RaiseComponentChange(bool isBeforeChange)
            {
                if ((this.editor != null) && (this.editor.Context != null))
                {
                    if (isBeforeChange)
                    {
                        this.editor.Context.OnComponentChanging();
                    }
                    else
                    {
                        this.editor.Context.OnComponentChanged();
                    }
                }
            }


            protected internal void RefreshContents()
            {
                this.OnEditValueChanged();
            }



            private void RemoveButton_click(object sender, EventArgs e)
            {
                int num1 = this.listbox.SelectedIndex;
                if (num1 != -1)
                {
                    BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.ListItem item1 = (BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.ListItem)this.listbox.Items[num1];
                    try
                    {
                        if (!base.CanRemoveInstance(item1.Value))
                        {
                            throw new Exception("CollectionEditorCantRemoveItem"+this.GetDisplayText(item1.Value) );
                        }
                        this.RaiseComponentChange(true);
                        base.DestroyInstance(item1.Value);
                        this.RaiseComponentChange(false);
                        this.listbox.Items.RemoveAt(num1);
                    }
                    catch (CheckoutException)
                    {
                    }
                    catch (Exception exception1)
                    {
                        this.DisplayError(exception1);
                    }
                }
                if (num1 < this.listbox.Items.Count)
                {
                    this.listbox.SelectedIndex = num1;
                }
                else if (this.listbox.Items.Count > 0)
                {
                    this.listbox.SelectedIndex = this.listbox.Items.Count - 1;
                }
                else
                {
                    this.UpdateEnabled();
                }
            }


            public void ResetListUI()
            {
                this.listbox.HorizontalExtent = 1;
            }


            private void UpButton_click(object sender, EventArgs e)
            {
                try
                {
                    int num1 = this.listbox.SelectedIndex;
                    if (num1 == 0)
                    {
                        return;
                    }
                    this.editor.OnInstanceOrderSwapped(num1, num1 - 1);
                    int num2 = this.listbox.TopIndex;
                    object obj1 = this.listbox.Items[num1];
                    this.listbox.Items[num1] = this.listbox.Items[num1 - 1];
                    this.listbox.Items[num1 - 1] = obj1;
                    if (num2 > 0)
                    {
                        this.listbox.TopIndex = num2 - 1;
                    }
                    this.listbox.ClearSelected();
                    this.listbox.SelectedIndex = num1 - 1;
                }
                catch (CheckoutException)
                {
                }
                catch (Exception exception1)
                {
                    this.DisplayError(exception1);
                }
            }

            private void UpdateEnabled()
            {
                bool flag1 = this.listbox.SelectedItem != null;
                this.removeButton.Enabled = ((this.editor.editValueMask & 8) != 0) && flag1;
                this.upButton.Enabled = flag1 && ((this.editor.editValueMask & 0x20) != 0);
                this.downButton.Enabled = flag1 && ((this.editor.editValueMask & 0x20) != 0);
                this.editButton.Enabled = ((this.editor.editValueMask & 4) != 0) && flag1;
                this.addButton.Enabled = (this.editor.editValueMask & 2) != 0;
                this.propertyBrowser.Visible = (this.editor.editValueMask & 1) != 0;
                this.propertiesLabel.Visible = (this.editor.editValueMask & 1) != 0;
                if (flag1)
                {
                    object[] objArray1;
                    if (this.IsImmutable)
                    {
                        objArray1 = new object[] { new BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.SelectionWrapper(base.CollectionType, base.CollectionItemType, this.listbox, this.listbox.SelectedItems) };
                    }
                    else
                    {
                        objArray1 = new object[this.listbox.SelectedItems.Count];
                        for (int num1 = 0; num1 < objArray1.Length; num1++)
                        {
                            objArray1[num1] = ((BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.ListItem)this.listbox.SelectedItems[num1]).Value;
                        }
                    }
                    int num2 = this.listbox.SelectedItems.Count;
                    this.propertiesLabel.Text = "CollectionEditorProperties "+this.GetDisplayText(((BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.ListItem)this.listbox.SelectedItem).Value);
                    if ((this.editor.editValueMask & 0x80) != 0)
                    {
                        this.propertyBrowser.SelectedObject = objArray1[0];
                    }
                    else
                    {
                        this.editor.propObject.CurrentObject = objArray1[0];
                        this.propertyBrowser.SelectedObject = this.editor.propObject;
                    }
                }
                else
                {
                    this.propertiesLabel.Text ="CollectionEditorPropertiesNone";
                    this.propertyBrowser.SelectedObject = null;
                }
            }



            private bool IsImmutable
            {
                get
                {
                    bool flag1 = true;
                    if (!TypeDescriptor.GetConverter(base.CollectionItemType).GetCreateInstanceSupported())
                    {
                        foreach (PropertyDescriptor descriptor1 in TypeDescriptor.GetProperties(base.CollectionItemType))
                        {
                            if (!descriptor1.IsReadOnly)
                            {
                                return false;
                            }
                        }
                    }
                    return flag1;
                }
            }

            protected string[] NewItemNames
            {
                get
                {
                    return this.editor.NewItemNames;
                }
            }


            private Button addButton;
            private ContextMenu addDownMenu;
            private TableLayoutPanel addEditTableLayoutPanel;
            private Button downButton;
            private Button editButton;
            private BaseDataCollectionEditor editor;
            private ListBox listbox;
            private static readonly double LOG10;
            private Label membersLabel;
            protected Button okButton;
            private TableLayoutPanel overarchingTableLayoutPanel;
            private const int PAINT_INDENT = 0x1a;
            private const int PAINT_WIDTH = 20;
            private Label propertiesLabel;
            private PropertyGrid propertyBrowser;
            private Button removeButton;
            private const int TEXT_INDENT = 1;
            private Button upButton;


            private class ListItem
            {
                public ListItem(object value, string name)
                {
                    this.value = value;
                    this.name = name;
                }


                public override string ToString()
                {
                    return this.name;
                }



                public TypeConverter Converter
                {
                    get
                    {
                        if (this.converter == null)
                        {
                            this.converter = TypeDescriptor.GetConverter(this.value);
                        }
                        return this.converter;
                    }
                }
                public UITypeEditor Editor
                {
                    get
                    {
                        if (this.editor == null)
                        {
                            this.editor = TypeDescriptor.GetEditor(this.value, typeof(UITypeEditor));
                            if (this.editor == null)
                            {
                                this.editor = this;
                            }
                        }
                        if (this.editor != this)
                        {
                            return (UITypeEditor)this.editor;
                        }
                        return null;
                    }
                }
                public object Value
                {
                    get
                    {
                        return this.value;
                    }
                    set
                    {
                        this.converter = null;
                        this.editor = null;
                        this.value = value;
                    }
                }

                private TypeConverter converter;
                private object editor;
                private string name;
                private object value;
            }

            private class SelectionWrapper : PropertyDescriptor, ICustomTypeDescriptor
            {
                public SelectionWrapper(Type collectionType, Type collectionItemType, Control control, ICollection collection)
                    : base("Value", new Attribute[] { new CategoryAttribute(collectionItemType.Name) })
                {
                    this.collectionType = collectionType;
                    this.collectionItemType = collectionItemType;
                    this.control = control;
                    this.collection = collection;
                    this.properties = new PropertyDescriptorCollection(new PropertyDescriptor[] { this });
                    this.value = this;
                    foreach (BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.ListItem item1 in collection)
                    {
                        if (this.value == this)
                        {
                            this.value = item1.Value;
                            continue;
                        }
                        object obj1 = item1.Value;
                        if (this.value != null)
                        {
                            if (obj1 == null)
                            {
                                this.value = null;
                                return;
                            }
                            if (this.value.Equals(obj1))
                            {
                                continue;
                            }
                            this.value = null;
                            return;
                        }
                        if (obj1 != null)
                        {
                            this.value = null;
                            return;
                        }
                    }
                }

                public override bool CanResetValue(object component)
                {
                    return false;
                }


                public override object GetValue(object component)
                {
                    return this.value;
                }


                public override void ResetValue(object component)
                {
                }


                public override void SetValue(object component, object value)
                {
                    this.value = value;
                    foreach (BaseDataCollectionEditor.BaseDataCollectionEditorCollectionForm.ListItem item1 in this.collection)
                    {
                        item1.Value = value;
                    }
                    this.control.Invalidate();
                    this.OnValueChanged(component, EventArgs.Empty);
                }

                public override bool ShouldSerializeValue(object component)
                {
                    return false;
                }

                AttributeCollection ICustomTypeDescriptor.GetAttributes()
                {
                    return TypeDescriptor.GetAttributes(this.collectionItemType);
                }


                string ICustomTypeDescriptor.GetClassName()
                {
                    return this.collectionItemType.Name;
                }

                string ICustomTypeDescriptor.GetComponentName()
                {
                    return null;
                }

                TypeConverter ICustomTypeDescriptor.GetConverter()
                {
                    return null;
                }

                EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
                {
                    return null;
                }
                PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
                {
                    return this;
                }

                object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
                {
                    return null;
                }

                EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
                {
                    return null;
                }

                EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
                {
                    return null;
                }

                PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
                {
                    return this.properties;
                }

                PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
                {
                    return this.properties;
                }

                object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
                {
                    return this;
                }


                public override Type ComponentType
                {
                    get
                    {
                        return this.collectionType;
                    }
                }
                public override bool IsReadOnly
                {
                    get
                    {
                        return false;
                    }
                }

                public override Type PropertyType
                {
                    get
                    {
                        return this.collectionItemType;
                    }
                }

               

                private ICollection collection;
                private Type collectionItemType;
                private Type collectionType;
                private Control control;
                private PropertyDescriptorCollection properties;
                private object value;
            }

            private class TypeMenuItem : MenuItem
            {
                public TypeMenuItem(Type itemType, EventHandler handler)
                    : base(itemType.Name, handler)
                {
                    this.itemType = itemType;
                }

                public TypeMenuItem(string name, Type itemType, EventHandler handler)
                    : base(name, handler)
                {
                    this.itemType = itemType;
                }

                public Type ItemType
                {
                    get
                    {
                        return this.itemType;
                    }
                }

                private Type itemType;
            }
        }

        

       
    }


  
}
